import { getGoodsInfo, addOrUpdateCatr } from "@/api/index";
//封装游客临时身份的模块
import { getuuid } from "@/untils/uuid";
const state = {
  goodsList: {},
  //游客临时身份
  uuid: getuuid(),
};
const mutations = {
  //设置产品数据
  getGoodsInfo(state, data) {
    state.goodsList = data;
  },
};
const actions = {
  //获取产品信息
  async getGoodsInfo({ commit }, skuid) {
    const res = await getGoodsInfo(skuid);
    if (res.code === 200) {
      commit("getGoodsInfo", res.data);
    }
  },
  //将产品添加到购物车
  // eslint-disable-next-line no-unused-vars
  async addGoodsToCart({ commit }, { skuId, skuNum }) {
    // eslint-disable-next-line no-unused-vars
    const res = await addOrUpdateCatr(skuId, skuNum);
    if (res.code == 200) return "ok";
    return Promise.reject(new Error("失败"));
  },
};
const getters = {
  // 面包屑
  categoryView(state) {
    //如果数据没加载完成就为空
    return state.goodsList.categoryView || {};
  },
  //产品信息
  skuInfo(state) {
    //如果数据没加载完成就为空
    return state.goodsList.skuInfo || {};
  },
  //产品售卖属性
  spuSaleAttrList(state) {
    return state.goodsList.spuSaleAttrList || [];
  },
};

export default {
  //开启命名空间
  namespaced: true,
  state,
  mutations,
  actions,
  getters,
};
